﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class FrmMttoPostulante : Form
    {
        [Dependency]
        public PersonaBL personaBL { get; set; }

        [Dependency]
        public PostulanteBL postulanteBL { get; set; }

        [Dependency]
        public IEstadoCivilBL estCivilBL { get; set; }

        [Dependency]
        public ISexoBL sexoBL { get; set; }

        [Dependency]
        public ILineaTelefonicaBL linTelfBL { get; set; }

        [Dependency]
        public ITelefonoBL telefonoBL { get; set; }

        public FrmMttoPostulante()
        {
            InitializeComponent();
            this.BuildUp();

            lINEATELEFONICABindingSource.DataSource = linTelfBL.GetAllFromLineaTelefonica();
        }

        bool isNew;

        //int sexoId;
        //int estCivilId;

        public void NuevoPostulante()
        {
            isNew = true;
            pERSONABindingSource.AddNew();
            pOSTULANTEBindingSource.AddNew();

            comboBox1.DataSource = sexoBL.GetAllFromSexo();
            comboBox2.DataSource = estCivilBL.GetAllFromEstadoCivil();
        }

        public POSTULANTE _postulante { get; set; }
        public void EditarPostulante()
        {
            isNew = false;
            _postulante.PERSONA.TELEFONOS = telefonoBL.GetAllFromTelefonoByPersona(_postulante.PERSONA.IdPersona);
            _postulante.PERSONA.SEXO = sexoBL.GetFromSexoById(_postulante.PERSONA.IdSexo);
            comboBox1.DataSource = sexoBL.GetAllFromSexoByDescripcion(_postulante.PERSONA.SEXO.Descripcion);
            _postulante.PERSONA.ESTADO_CIVIL = estCivilBL.GetFromEstadoCivilById(_postulante.PERSONA.IdEstadoCivil);
            comboBox2.DataSource = estCivilBL.GetAllfromEstadoCivilByDescripcion(_postulante.PERSONA.ESTADO_CIVIL.Descripcion);


            pOSTULANTEBindingSource.DataSource = _postulante;
            pERSONABindingSource.DataSource = _postulante.PERSONA;

            apePaternoTextBox.ReadOnly = false;
            apeMaternoTextBox.ReadOnly = false;
            nombresTextBox.ReadOnly = false;
            direccionTextBox.ReadOnly = false;
            emailTextBox.ReadOnly = false;
            fechaNacimientoDateTimePicker.Enabled = true;
            comboBox1.Enabled = true;
            comboBox2.Enabled = true;
        }
        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var persona = pERSONABindingSource.Current as PERSONA;
            var postulante = pOSTULANTEBindingSource.Current as POSTULANTE;
            var msg = "ERROR";

            persona.FechaNacimiento = fechaNacimientoDateTimePicker.Value;
            
            //asignando TRABAJADOR a person
            persona.POSTULANTEs = postulante;

            if (persona != null)
            {
                if (isNew == true)
                {
                    try
                    {
                        postulante.IdPostulante = persona.IdPersona;
                        postulante.Observacion = observacionTextBox.Text.ToUpper();
                        postulante.Disponibilidad = disponibilidadTextBox.Text.ToUpper();

                        postulanteBL.AddPostulante(postulante);
                        MessageBox.Show("Registro Correcto", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                 }
                 
                
                if (isNew == false)
                {
                    try
                    {
                        postulante.IdPostulante = persona.IdPersona;
                        postulante.Observacion = observacionTextBox.Text.ToUpper();
                        postulante.Disponibilidad = disponibilidadTextBox.Text.ToUpper();
                        postulanteBL.UpdatePostulante(postulante);
                        MessageBox.Show("Modificación Correcta", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    
                }
            }
        }
        
        private void FrmMttoPostulante_Load(object sender, EventArgs e)
        {
            //pOSTULANTEBindingSource.AddNew();
            //pERSONABindingSource.AddNew();
        }

        private void comboBox1_Click(object sender, EventArgs e)
        {
            comboBox1.DataSource = sexoBL.GetAllFromSexo();
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue != null)
            {
                //var a = (from c in sexoBL.GetAllFromSexoByDescripcion(comboBox1.SelectedValue.ToString())
                //         select c.IdSexo).SingleOrDefault();

                idSexoTextBox.Text = comboBox1.SelectedValue.ToString();
                //sexoId = a;
            }
        }
        private void comboBox2_Click(object sender, EventArgs e)
        {
            comboBox2.DataSource = estCivilBL.GetAllFromEstadoCivil();
        }
        private void comboBox2_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedValue != null)
            {
                //var a = (from c in estCivilBL.GetAllfromEstadoCivilByDescripcion(comboBox2.SelectedValue.ToString())
                //         select c.IdEstadoCivil).SingleOrDefault();

                idEstadoCivilTextBox.Text = comboBox2.SelectedValue.ToString();
                //estCivilId = a;
            }
        }


        private void VerificarDNI()
        {
            if (dniTextBox.Text.Length != 0)
            {
                if (dniTextBox.Text.Length == 8)
                {
                    var persona = personaBL.GetFromPersonaByDni(dniTextBox.Text);
                    if (persona != null)
                    {
                        var postulante = postulanteBL.GetFromPostulanteByIdPersona(persona.IdPersona);

                        if (persona != null && postulante != null)
                        {
                            pERSONABindingSource.DataSource = persona;
                            MessageBox.Show("Postulante existente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Close();
                        }
                        else if (persona != null && postulante == null)
                        {
                            pERSONABindingSource.DataSource = persona;
                            apePaternoTextBox.ReadOnly = true;
                            apeMaternoTextBox.ReadOnly = true;
                            nombresTextBox.ReadOnly = true;
                            direccionTextBox.ReadOnly = true;
                            emailTextBox.ReadOnly = true;
                            fechaNacimientoDateTimePicker.Enabled = false;
                            comboBox1.Enabled = false;
                            comboBox2.Enabled = false;
                        }
                        
                    }
                    else 
                    {
                        apePaternoTextBox.ReadOnly = false;
                        apeMaternoTextBox.ReadOnly = false;
                        nombresTextBox.ReadOnly = false;
                        direccionTextBox.ReadOnly = false;
                        emailTextBox.ReadOnly = false;
                        fechaNacimientoDateTimePicker.Enabled = true;
                        comboBox1.Enabled = true;
                        comboBox2.Enabled = true;
                    }
                }
                else
                {
                    MessageBox.Show("Longitud de DNI incorrecta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("Ingrese un DNI", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnVerificarDni_Click(object sender, EventArgs e)
        {
            VerificarDNI();
        }

        private void dniTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void apePaternoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var amberson = Convert.ToString("&");
            var par1 = Convert.ToString("(");
            var par2 = Convert.ToString(")");
            var punto = Convert.ToString(".");
            var guion = Convert.ToString("-");
            var coma = Convert.ToString(",");


            string cadena = amberson + par1 + par2 + punto + guion + coma + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void apeMaternoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var amberson = Convert.ToString("&");
            var par1 = Convert.ToString("(");
            var par2 = Convert.ToString(")");
            var punto = Convert.ToString(".");
            var guion = Convert.ToString("-");
            var coma = Convert.ToString(",");


            string cadena = amberson + par1 + par2 + punto + guion + coma + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void nombresTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var amberson = Convert.ToString("&");
            var par1 = Convert.ToString("(");
            var par2 = Convert.ToString(")");
            var punto = Convert.ToString(".");
            var guion = Convert.ToString("-");
            var coma = Convert.ToString(",");


            string cadena = amberson + par1 + par2 + punto + guion + coma + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void dniTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                VerificarDNI();
            }
        }

        private void dniTextBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                VerificarDNI();
            }
        }

        
    }
}
